<script setup lang="ts">
import { get, loadFile, message, post } from '@/utils'
import { Market, marketMap } from '@/enum/market'
// import Modal from './components/Modal.vue'
import useModal from '@/hooks/useModal'
import { BtnPermission } from '@/enum/permission'
import useTable from '@/hooks/useTable'
import useValueEnum from '@/hooks/useValueEnum'
const { store } = useModal()
const router = useRouter()
const { formatColumns } = useTable()
const searchQuery = reactive({})

const formRef = ref()
const actionRef = ref()
const pagination = {
  showPageSize: true,
  pageSize: 20,
}
const request = (params: any, sort?, filters?): Promise<any> => {
  return new Promise((resolve, reject) => {
    get(
      {
        url: '/server-shop/admin/publicity/page',
        data: {
          size: params.pageSize,
          ...params,
          beginTimeBegin: params.beginTime ? params.beginTime[0] + ' 00:00:00' : '',
          beginTimeEnd: params.beginTime ? params.beginTime[1] + ' 23:59:59' : '',
          endTimeBegin: params.endTime ? params.endTime[0] + ' 00:00:00' : '',
          endTimeEnd: params.endTime ? params.endTime[1] + ' 23:59:59' : '',
        },
      },
      ({ data, code }) => {
        resolve({
          data: data.records,
          total: data.total,
          success: code == 200,
        })
      }
    )
  })
}
const { valueEnum: seller } = useValueEnum('/server-shop/admin/seller/list')

const columns = formatColumns([
  { title: '活动ID', dataIndex: 'id', width: 200 },
  { title: '归属商户', dataIndex: 'sellerId', width: 200, valueEnum: seller, hideInSearch: false },
  { title: '活动类型', dataIndex: 'type', width: 200, valueEnum: marketMap, hideInSearch: false },
  { title: '标题', dataIndex: 'title', hideInSearch: false, width: 200, ellipsis: true },
  {
    title: '排序',
    dataIndex: 'sort',
    width: 200,
    sorter: true,
  },
  { title: '开始时间', dataIndex: 'beginTime', width: 200, valueType: 'dateRange', hideInSearch: false },
  { title: '结束时间', dataIndex: 'endTime', width: 200, valueType: 'dateRange', hideInSearch: false },
  { title: '创建时间', dataIndex: 'createTime', width: 200 },

  {
    title: '操作',
    dataIndex: 'option',
    width: 250,
    fixed: 'right',
    btnList: [
      {
        name: '活动商品',
        permission: BtnPermission.查看,
        handle: ({ record }) => {
          router.push({
            name: 'marketActivityGoods',
            query: {
              publicityId: record.id,
              type: record.type,
            },
          })
        },
      },
    ],
  },
])
</script>

<template>
  <div class="page">
    <ProTable :columns="columns" :request="request" row-key="id" :scroll="{ x: 1000 }" :form-ref="ref => (formRef = ref)" :pagination="pagination" header-title="" column-resizable :params="searchQuery" :bordered="{ cell: true }" :action-ref="ref => (actionRef = ref)">
      <template #option-render="{ dom, reset }">
        <component :is="dom[1]" />
        <component :is="dom[0]" />
        <!-- <a-button type="primary" @click="store.handleShow({})">
          <template #icon>
            <icon-plus />
          </template>
          新建
        </a-button> -->
      </template>
    </ProTable>
    <!-- <Modal v-if="store.show" v-model:show="store.show" :id="store.id" @confirm="actionRef.reload" /> -->
  </div>
</template>
